<?php
/**
 * 业务模型基础类
 *   
 * @author wenqiang@6rooms.com
 * @version $Id: Base.class.php,v 1.0 2013/11/28 17:08:30 gaowenqiang Exp $   
 * @package hw.service   
 *
 */
class Hw_Service_Base
{   
	const ERROR_RUNTIME_HANDLE_BREAK = 1;
	const ERROR_RUNTIME_HANDLE_CONTINUE = 2;

	/**
	 * 配置参数
	 *
     * @var array
	 *
     */
    protected $options = array();

	/**
	 * 构造器
	 *
	 * @param mixed $args 参数
	 * @return void
	 *
	 */
	public function __construct($args = null)
	{
		$this->init($args);
	}

	/**
	 * 初始化
	 *
	 * @param mixed $args 参数
	 * @return void
	 */
	protected function init($args = null)
	{
		
	}

	protected function addFields($fields)
	{
		if (is_array($fields)) {
			if (!empty($fields)) {
				foreach ($fields as $field) {
					$this->options[$field] = null;
				}
			}
		} else {
			$field = $fields;
			$this->options[$field] = null;
		}
	}
	
    /**
	 * 设置配置参数
	 *
     * @param array $option 配置参数 
	 * @param int $errorBreak 出错时后续代码是否运行
	 * @return bool
	 *
	 */
    protected function setOptions($options, $handle = self::ERROR_RUNTIME_HANDLE_BREAK)
    {
		$errorCode = 0;// 错误代码
		if (is_array($options) && !empty($options)) {
			foreach ($options as $key => $val) {
				if (array_key_exists($key, array($this->options))) {
					$method = 'set' . ucfirst($key);
					if (method_exists($this, $method)) {// 调用自定义参数赋值方法
						$errorCode = call_user_func(array($this, $method), $val);
					} else {
						$errorCode = $this->setOption($key, $val);
					}
					/*
					if ($errorCode < 0 && $handle === self::ERROR_RUNTIME_HANDLE_BREAK) {
						// write log (error code + message)...
						break;
					}
					*/
				}
			}
		}
	/*
		if ($errorCode < 0) {
			// throw Exception(code)
			return false;
		};
	 */
		return true;
    }
	
	protected function setOption($key, $val)
	{
		$this->options[$key] = $val;
		return true;
	}
	
	protected function clearOption()
	{
		$this->options = null;
	}

	/**
	 * 获取指定name的值
	 * 
	 * @param string $name
	 * @return mixed
	 *
	 */
	protected function getOption($name, $key = null)
	{
		if (array_key_exists($name, $this->options)) {
			$data = $this->options['name'];
			if (!is_null($key)) {
				if (is_string($key)) {
					if (strlen($key)) {
						if (is_array($data) && !empty($data) && array_key_exists($key, $data)) {// val为数组
							return $data[$key];
						}
					}
				} elseif (is_array($key) && !empty($key)) {
					$tmp = array();
					foreach ($key as $fieldKey) {
						if (array_key_exists($fieldKey, $data)) {
							$tmp[$fieldKey] = $data[$fieldKey];
						}
					}
					return $tmp;
				}
				
			} else {
				return $data;
			}
		}

		return false;
	}
	
	/**
	 * 自动加载内部类(service, model)
	 * 
	 * @param string $name 
	 * @return mixed (Class Type: Hw_Service_Base|Hw_Model_Base| other Class)
	 *
	 */
	/*
	protected function __get($name)
	{
		$matches = array();
		if (preg_match('/^(model|service)_([\w\_]+)$/', $name, $matches) != 0) {
			$fun = isset($matches[1]) && $matches[1] ? strtolower($matches) : null;
			if (strlen($fun) && in_array($fun, array('model', 'service'))) {
				$param = isset($matches[2]) && $matches[2] ? $matches[2] : null;// 短类名(无前辍)
				return call_user_func(array($this, $fun), $param);
			}
		}

		return false;
	}
	*/

	/**
	 * 获取对象
	 *
	 * @param string $className 类名
	 * @param mixed $args 参数
	 * @return object
	 *
	 */
	protected function object($className, $args = null, $cache = true)
	{
		if (!strlen($className)) {
			return false;
		}
		
		return Hw_Factory::object($className, $arg, $cache);
	}
	

	// 错误日志
	protected function error($message, $context = array())
	{
		if (!empty($context)) {
			foreach ($conext as $fieldName => $fieldValue) {
				$message = str_replace("\{$fieldName\}", $fieldValue, $message);
			}
		}

		$level = 'error';
		$now = date('Y-m-d H:i:s');
		$content = sprintf('[%s][%s]%s', $level, $now, $message);

		$path = LOG_DIR
		$filename = 'error.log';

		return file_put_contents($path . $filename, $content, FILE_APPEND);
	}

}

